package com.study.whyyy.aspect;

import com.study.whyyy.annotations.CustomLogAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author whyyy
 * @create 2025/2/13 14:50
 */

@Slf4j
@Aspect
@Component
public class CustomLogAspect {
    @Around("@annotation(customLogAnnotation)")
    public Object logAround(ProceedingJoinPoint proceedingJoinPoint, CustomLogAnnotation customLogAnnotation) throws Throwable {
        Object result = null;
        if (customLogAnnotation.enable()) {

            long startTime = System.currentTimeMillis();

            //1.方法执行前处理逻辑
            System.out.println("Before method execution: " + proceedingJoinPoint.getSignature().getName());

            //2.放行,执行主业务逻辑
            result = proceedingJoinPoint.proceed();

            //3.方法执行后处理逻辑
            System.out.println("After method execution:"+ customLogAnnotation.value());

            long endTime = System.currentTimeMillis();
            System.out.println("----costTime: "+(endTime - startTime) +" 毫秒");
            return result;
        }else {
           return proceedingJoinPoint.proceed();
        }
    }
}
